#include <p18f2620.h>
#include "menu.h"
#include "vfd.h"
#include "timer.h"
#include "onewire.h"
#include "uart.h"
#include "button.h"
#include "indicator.h"
#include "heater.h"
#include "onestep.h"

/* Globals */
extern unsigned char line_cur, line_old;
extern unsigned char active_old;
extern unsigned char btn;
extern unsigned char state, state_prev;
extern unsigned char run;
extern unsigned int temp_raw;
extern unsigned char countdown_state;

#define TEMP_ONE 2600
#define TEMP_TWO 3300
#define TEMP_TWO_DELTA 63

unsigned int TempTwo = TEMP_TWO; //[16] = { 3300, 3238, 3175, 3113, 3050, 2988, 2925, 2863, 2800, 2738, 2675, 2613, 2550, 2488, 2425, 2363, 2300 };


struct MenuEntry DurationOneMenu[] = {  // Auffrischen - Dauer
	{ (const rom char*)"1. Stufe - Dauer?", Text, 0xFF },
	{ (const rom char*)"4 Stunden", Item, 0x03 },
	{ (const rom char*)"5 Stunden", Item, 0x03 },
	{ (const rom char*)"6 Stunden", Item, 0x03 },
	{ (const rom char*)"Abbrechen", Exit, 0xFF }
};

struct MenuEntry RefreshMenu[] = {  // Auffrischen - Status
	{ "Auffrischen", Text, 0xFF },
	{ "S:XX.XX\\h27C", Text, 0xFF }
};

struct MenuEntry DurationTwoMenu[] = {  // Fuehren - Dauer
    { (const rom char*)"2. Stufe - Dauer?", Text, 0xFF },
    { (const rom char*)"16 Stunden", Item, 0x05 },
    { (const rom char*)"17 Stunden", Item, 0x05 },
    { (const rom char*)"1. Stufe wdh.", Item, 0x02 },
    { (const rom char*)"Abbrechen", Exit, 0xFF }
};

struct MenuEntry FuehrungMenu[] = {
    { "F\\h81hrung", Text, 0xFF },
    { "S:XX.XX\\h27C", Text, 0xFF }
};

void onestep_one(void) {    // Auswahl Auffrischung
    heater_off();
    menu_management(5, DurationOneMenu);
}

void onestep_duration_one(void) {   // Status Auffrischung
    if((btn & BUTTON1) == 1) {
        if(run == 1) {
            state_prev = state;
            menu_set_state(0xF0);
        }
    }
    else {
        if(run == 0) {
            switch(active_old) {
                case 0: //               H  M  S
                    timer_countdown_init(0, 0, 3);  // TODO
                    break;
                case 1:
                    timer_countdown_init(4, 59, 59);
                    break;
                case 2:
                    timer_countdown_init(5, 59, 59);
                    break;
            }
            heater_set_temp(TEMP_ONE);
            timer_countdown_start();
            run = 1;
        }
        else {
            menu_show(RefreshMenu, &line_cur, &line_old);
            heater_manage(temp_raw);

            // Time management
            vfd_pos(12);
            if(countdown_state == 0) {    // Zeit abgelaufen!
                timer_countdown_stop();
                idctr_beep(4, 4000);
                menu_set_state(0x04);
            }

            // Time output
            timer_putcountdown();

            // Temperature Target output
            vfd_puttemp(TEMP_ONE, 2, 1);

            // Temperature Actual output
            ow_puttemp(temp_raw, 11, 1);
        }
    }
}

void onestep_two(void) {    // Fuehrung - Dauer
    heater_off();
    menu_management(5, DurationTwoMenu);
}


void onestep_duration_two(void) {   // Fuehrung - Status
    if((btn & BUTTON1) == 1) {
        if(run == 1) {
            state_prev = state;
            menu_set_state(0xF0);
        }
    }
    else {
        if(run == 0) {
            switch(active_old) {
                case 0: //                H  M  S
                    timer_countdown_init(14, 8, 3);
                    break;
                case 1:
                    timer_countdown_init(16, 59, 59);
                    break;
            }
            TempTwo = TEMP_TWO - TEMP_TWO_DELTA;
            heater_set_temp(TempTwo);
            timer_countdown_start();
            run = 1;
        }
        else {
            menu_show(FuehrungMenu, &line_cur, &line_old);
            heater_manage(temp_raw);

            // Time management
            vfd_pos(12);
            if(countdown_state == 0) {    // Countdown finished
                timer_countdown_stop();
                idctr_beep(4, 4000);
                menu_set_state(0xF1);
            }
            else if(countdown_state == 2) { // One hour over
                TempTwo = TempTwo - TEMP_TWO_DELTA;
                heater_set_temp(TempTwo);
            }

            // Time output
            timer_putcountdown();

            // Temperature Target output
            vfd_puttemp(TempTwo, 2, 1);

            // Temperature Actual output
            ow_puttemp(temp_raw, 11, 1);
        }
    }
}